---
title: Univer Docs API
---

Univer Docs provides professional document typesetting capabilities, with concepts closely aligned with Microsoft Word.

## Operate Documents

In the command system, `unitId` corresponds to the unique identifier of the document, and documents do not have a `subUnitId`.

The text content of the document is stored in the `body.dataStream` string, which does not contain style information.

Style information is stored separately in another data structure and is associated with the text content through index references.

For elements such as line breaks, page breaks, sections, paragraphs, and tables, different special characters are used to mark them in the text content. These special characters are converted to corresponding elements during rendering.

<Callout>
  Want to learn more about the design of document data structures? We recommend reading [Univer Document Architecture and Module Design](/blog/univer-doc-architecture) and [An Initial Exploration of Univer Document Typesetting Design](/blog/doc-typesetting-design).
</Callout>

### Create Document

The `univer.createUnit` method is used to create a new document instance. When the first parameter is `UniverInstanceType.UNIVER_DOC`, it indicates that a document instance is being created.

The second parameter is an optional `DocumentDataModel` object that contains the initial data for the document. If `{}` is passed, an empty document is created.

The creation returns a `DocumentDataModel` object that contains the unique identifier `unitId` of the document and other related information.

```typescript
// [!code word:data]
univer.createUnit(UniverInstanceType.UNIVER_DOC, data)
```

### Get Document unitId

```typescript
const doc = univerAPI.getActiveDocument()
const unitId = doc?.getId()
```

### Get Document Data

```typescript
const doc = univerAPI.getActiveDocument()
const saveData = doc.getSnapshot()
```

### Dispose Document

When we no longer need the document, we can call the `dispose` method of the Univer instance to destroy the instance.

```typescript
univer.dispose()
```

## Operate Text

Modify text elements in the rich text area

### Insert Text

To append specified text to the end of this text area.

```typescript
const doc = univerAPI.getActiveDocument()
doc?.appendText('Univer')
```

### Delete Text

Delete the character before the cursor

```typescript
univerAPI.executeCommand('doc.command.delete-left')
```

Delete the character after the cursor

```typescript
univerAPI.executeCommand('doc.command.delete-right')
```

## Modify Style

Set the font color for the selected text

```typescript
univerAPI.executeCommand('doc.command.set-inline-format-text-color', { value: '#ff0000' })
```

## Insert Page Break

The `\f` character is a page break, used to insert a page break in the document.

```typescript
const doc = univerAPI.getActiveDocument()
doc?.appendText('\f')
```

## Insert List

Insert an unordered list at the cursor's current paragraph; repeated calls will toggle the unordered list off.

```typescript
univerAPI.executeCommand('doc.command.list-operation', { listType: 'BULLET_LIST' })
```

Insert an ordered list at the cursor's current paragraph; repeated calls will toggle the ordered list off.

```typescript
univerAPI.executeCommand('doc.command.list-operation', { listType: 'ORDER_LIST' })
```
